<?php
class PayAction extends Action {
	private $Alipay;
	public function _initialize(){
		vendor('Alipay.alipay');
		$this->Alipay = new Alipay();
	}
	
	public function alipay ($data){
		$this->Alipay->alipayTo($data);
	}
	
	//同步
	public function return_url (){
		unset($_GET['_URL_']);
		if ($this->Alipay->return_url()){
			$this->_doSuccess();	
		}
	}
	
	//异步
	public function notify_url (){
		if ($this->Alipay->notify_url()){
			$this->_doSuccess();	
		}
	}
	
	//验证成功
	private function _doSuccess(){
		$this->_updateOrder();	//更新order表
		$this->_updateUser();	//更新user表
		$this->assign('waitSecond',3);
		$this->success('现在您是高级会员了！请退出后重新登录~','/');
	}
	
	private function _updateOrder(){
		$out_trade_no	= $this->_request('out_trade_no');	//获取订单号
		$trade_no		= $this->_request('trade_no');		//获取支付宝交易号
		$total_fee		= $this->_request('total_fee');		//获取总价格
		$Order			= M('Order');
		$row			= $Order -> field('order_status') -> where(array('order_no'=>$out_trade_no)) -> find();
		if ($row['order_status'] != 1){
			$Order	-> where(array('order_no'=>$out_trade_no)) -> save(array('trade_no'=>$trade_no,'order_status'=>1));
		}
	}
	
	private function _updateUser(){
		$User				= M('User');
		$User_id			= M('Order')->field('user_id')->where(array('order_no'=>$this->_request('out_trade_no')))->find();
		$month				= $this->_request('extra_common_param');
		$user_advance_time	= strtotime($month.' month');
		$User				->where('user_id = '.$User_id['user_id']) -> save(array('user_advance'=>1,'user_advance_time'=>$user_advance_time));
		$_SESSION['user']['user_advance'] 		= 1;
		$_SESSION['user']['user_advance_time']	= $user_advance_time;
	}
}